package com.atguigu.oss.controller;

import com.atguigu.commonutils.R;
import com.atguigu.oss.service.OssService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * @author: He Wentao
 * @date: 2022/8/19 22:48
 */
@RestController
@RequestMapping("oss/file")
@CrossOrigin
public class OSSController {

    @Resource
    private OssService ossService;

    // 上传用户头像
    @PostMapping("/upload")
    public R uploadOssFile(@RequestParam("file")MultipartFile file){

        if (!file.isEmpty()){
            // 使用 MultipartFilter 获取到上传的文件，然后调用 service 保存到 OSS 云储存
            return ossService.uploadFileAvatar(file) ;
        }else {
            return R.error().message("上传的文件为空");
        }
    }

    @DeleteMapping("/delete/{url}")
    public R deleteFile(@PathVariable("url") String url){
        return ossService.deleteAvatar(url);
    }
}
